/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatNI;

import java.io.IOException;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import ChatController.*;


/**
 *
 * @author doutre
 */
public class UDPRec extends Thread {

    private DatagramSocket datagrameSocket; //envoie et reception des datagrames
    private int portNum;
    private int taille = 10000;
    ChatController cc;

    public DatagramSocket getDatagrameSocket() {
        return datagrameSocket;
    }
    
    

    public UDPRec(int port, ChatController cc) throws SocketException {
        super();
        this.datagrameSocket = new DatagramSocket(port);
        this.portNum = port;
        this.cc = cc;

    }

    @Override
    public void run() {

        while (cc.getRunning()) {
            try {
                DatagramPacket dp = new DatagramPacket(new byte[taille], taille);
                datagrameSocket.receive(dp);
                byte[] tab = dp.getData();

                cc.receive(tab);

            } catch (IOException ex) {
                System.out.println("Erreur lecture ");
            }

        }
        datagrameSocket.close();
        System.out.println("écoute réseau terminée");

    }
}
